package unicall.em.utils;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;



public class HttpClientUtil {
    public String doPost(String url,Map<String,String> map){
         HttpClient httpClient = null;
         HttpPost httpPost = null;
         String result = null;
         httpClient = HttpClients.createDefault();
         httpPost = new HttpPost(url);
         HttpResponse response = null;
         try {
         List<NameValuePair> list = new ArrayList<NameValuePair>();
         for(Entry<String,String> entry : map.entrySet()){
          list.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));   
         }
         if(!list.isEmpty()){
               UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "utf-8");
               httpPost.setEntity(entity);
         }
         response = httpClient.execute(httpPost);
         if(null!=response){
             HttpEntity entity = response.getEntity();
             if(null!=entity){
                 result = EntityUtils.toString(entity,"utf-8");
             }
         }
         }catch(Exception e){
             e.printStackTrace();
         }
         return result;
    }

    

    
    
    
    
   
    
    
}
